var express = require('express');
var router = express.Router();
const db = require('../db/index')
const url = require('url')
var querystring = require("querystring");
var token = require('./login')

// 渲染页面和文章
router.get('/', function (req, res, next) {

  const userinfo = req.body
  //获取返回的url对象的query属性值 
  var arg = url.parse(req.url).query;

  //将arg参数字符串反序列化为一个对象
  var params = querystring.parse(arg);
  const sqlStr = 'select * from article ORDER BY `article`.`id` DESC; select * from header; select * from title; SELECT * FROM `users` WHERE id = ?;SELECT  COUNT(DISTINCT tag) as tag from article;'
  const sqldata = [params.usesid]
  db.query(sqlStr, sqldata, (err, results) => {
    //执行sql语句成功，但是获取到的数据小于等于0
    if (results.length <= 0) return res.cc('查询失败！')
    const wz = results[0].length
    const img = '/images/Snipaste_2023-04-10_19-33-53.png'
    let name = '未登录'
    res.render('index', { artcate: results[0], header: results[1], wz, title: results[2], img: params.usesid == undefined ? img : results[3][0].images, username: params.usesid == undefined ? name : results[3][0].username,tag:results[4][0].tag})

  })


});

// 发布的api
router.post('/publish', function (req, res) {
  const userinfo = req.body
  const sqlStr = 'insert into article set ?'
  db.query(sqlStr, { title: userinfo.title, content: userinfo.content, author: userinfo.author, time: userinfo.time }, (err, results) => {

    // 判断sql语句是否正确
    if (err) return res.cc(err)

    // 判断是否插入成功
    if (results.affectedRows !== 1) {
      return res.cc('注册新用户失败，请稍后在试！')
    }
    res.cc('注册成功！', 0)

  })
});

// 分类重新渲染页面
router.post('/Re-render', (req, res) => {
  const sqlStr = `SELECT * FROM article WHERE tag = '${req.body.tag}' ORDER BY article.time DESC`;

  db.query(sqlStr, (err, results) => {
    if (err) {
      return console.log(err.message);
    } else {
      res.send({
        status: 200,
        message: '分类查询成功',
        data: results
      });
    };
  })
});

module.exports = router;
